home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / ScianNames.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  323 lines

  1. /* ScianNames.c: John R. Murray. Various string - ID number association
  2.  * mechanisms. This stuff is extremely dusty and largely unused
  3.  * and unusable, with the primary exception of GetInternalString.
  4.  */
  5.  
  6. #include "Scian.h"
  7. #include "ScianTypes.h"
  8. #include "ScianNames.h"
  9. #include "ScianGarbageMan.h"
  10. #include "ScianWindows.h"
  11. #include "ScianObjWindows.h"
  12. #include "ScianDialogs.h"
  13. #include "ScianIDsDefiner.h"
  14.  
  15. IDArrayStruct IDArray[MAXIDNUM];
  16.  
  17. /* global Name tree, associates string names with ID numbers */
  18. NameNodePtr    IDsTree = (NameNodePtr) NIL;
  19.  
  20. /* ComparePtrs returns true iff two pointers address the same location */
  21. #define ComparePtrs(obj1,obj2) ( (void *) (obj1) == (void *) (obj2))
  22.  
  23. /* AssignPtr assigns one pointer to another regardless of types pointed to */
  24. #define AssignPtr(p1,p2) ( (void *) (p1) = (void *) (p2))
  25.  
  26. /* id 0="undefined", id 1 to MAXPREDEF..=predefined, >MAXPRE.. "user" */
  27. NameTyp    SaveLastID = MAXPREDEFIDNUM;
  28.  
  29. /* forward define */
  30. void    InitIDsTree();
  31.  
  32. void    FreeNameTree(node)
  33. NameNodePtr     *node;
  34. {
  35.     if (! *node)
  36.     return;
  37.  
  38.     FreeNameTree( &((*node)->left) );       /* clear the subtrees */
  39.     FreeNameTree( &((*node)->right) );
  40.     if ((*node)->stringPtr)
  41.     {
  42.     Free((*node)->stringPtr);       /* clear this node's string */
  43.     }
  44.     Free(*node);                    /* free this node */
  45.     *node = (NameNodePtr) NIL;
  46. }
  47.  
  48. void    InitNames()
  49. {
  50.     int i;
  51.  
  52.     for (i = 0; i < MAXIDNUM; ++i)
  53.     {
  54.     IDArray[i].str = NIL;
  55.     IDArray[i].thing = NULLOBJ;
  56.     }
  57.     InitIDsTree();
  58. #ifdef DEBUG
  59.     PrintIDTree();
  60. #endif
  61. }
  62.  
  63. extern long globalRefCount;
  64.  
  65. extern int deleteCount;
  66. extern int trivialDeleteCount;
  67. extern int noDeleteCount;
  68. extern int missedDeleteCount;
  69.  
  70. void    KillNames()
  71. {
  72.     int    i;
  73.  
  74.     FreeNameTree(&IDsTree);
  75.  
  76.     deleteCount = deleteCount; /* dummy statements to put a dbx trap on */
  77.     deleteCount = deleteCount;
  78. }
  79.  
  80. NameNodePtr    NewNameNode(str,id)
  81. char    *str;
  82. NameTyp    id;
  83. /* NewNameNode:    creates a new node for the internal name tree */
  84. {
  85.     NameNodePtr    newNode;
  86.  
  87.     if (!(newNode = (NameNodePtr) Alloc(sizeof(NameNode)) ))
  88.     {
  89.     OMErr();
  90.     return (NameNodePtr) NIL;
  91.     }
  92.     /* newNode->stringPtr = str; */
  93.     if(!(newNode->stringPtr = (char *) Alloc(strlen(str)+1) ))
  94.     {
  95.     OMErr();
  96.     return (NameNodePtr) NIL;
  97.     }
  98.     strcpy(newNode->stringPtr,str);
  99.     newNode->id = id;
  100.     newNode->left = (struct NameNodeStr *) NIL;
  101.     newNode->right = (struct NameNodeStr *) NIL;
  102.     IDArray[id].str = newNode->stringPtr;
  103.     return newNode;
  104. }
  105.  
  106. NameTyp    InternalNameToID(whichTree, namestr, theComp)
  107. NameNodePtr    *whichTree;
  108. char        namestr[];
  109. int        (* theComp)();
  110.  
  111. /* NameToID:    given string name, find the ID of a thing. If string is not
  112.     found, insert it in the tree, with new ID */
  113. {
  114.     NameNodePtr    *runner;
  115.     Bool    notdone = true;
  116.     int        cmpval;
  117.  
  118.     runner = whichTree;
  119.     while(notdone)
  120.     {
  121.     if (! *runner)
  122.     {
  123.         notdone = false;
  124.     }
  125.     else if (0 == (cmpval = theComp((*runner)->stringPtr,namestr)) )
  126.     {
  127.         /* found it */
  128.         notdone = false;
  129.     }
  130.     else if (cmpval > 0)
  131.     {
  132.         /* change runner, continue */
  133.         runner = &(*runner)->left;
  134.     }
  135.     else /* cmpval < 0 */
  136.     {
  137.         /* change runner, continue */
  138.         runner = &(*runner)->right;
  139.     }
  140.     }
  141.     if (*runner)
  142.     {
  143.     return (*runner)->id;
  144.     }
  145.     else
  146.     {
  147.     return 0;
  148.     }
  149. }
  150.  
  151. char numberBuffer[20];
  152.  
  153. char    *IDToName(id)
  154. NameTyp    id;
  155. /* IDToName:    given thing's ID, find the string name. Returns null string if
  156.     not found */
  157. {
  158.     if (IDArray[id].str)
  159.     {
  160.     return IDArray[id].str;
  161.     }
  162.     else
  163.     {
  164.     sprintf(numberBuffer, "%d", id);
  165.     return numberBuffer;
  166.     }
  167. }
  168.  
  169. void    InternalDefineName(whichTree,namestr,id)
  170. NameNodePtr    *whichTree; /* pointer to the tree pointer */
  171. char    *namestr;
  172. NameTyp    id;
  173. /* DefineName:    given string name and id no., associate them with each other.
  174.     This will either create a new NameNode, or alter an old one. */
  175. {
  176.     NameNodePtr    *runner;
  177.     Bool    notdone = true;
  178.     int        cmpval;
  179.  
  180.     if (id >= MAXIDNUM)
  181.     {
  182.     ReportError("InternalDefineName","Internal Error: ID number too large");
  183.     return;
  184.     }
  185.  
  186.     runner = whichTree;
  187.     while(notdone)
  188.     {
  189.     if (! *runner)
  190.     {
  191.         /* new node */
  192.         if (!(*runner = NewNameNode(namestr,id)))
  193.         return;        /* ran out of memory */
  194.         notdone = false;
  195.     }
  196.     else if (0 == (cmpval = strcmp((*runner)->stringPtr,namestr)) )
  197.     {
  198.         /* found old association, redefine it */
  199.         /* NOTE: have to define what happens if IDArr contains
  200.         more than just stringPtr */
  201.         /* NOTE: there's a hole here through which you could
  202.         throw away memory */
  203.         (*runner)->id = id;
  204.         IDArray[id].str = Alloc(strlen(namestr) + 1);
  205.         if (IDArray[id].str == NIL)
  206.         {
  207.         OMErr();
  208.         return;
  209.         }
  210.         strcpy(IDArray[id].str, namestr);
  211.         notdone = false;
  212.     }
  213.     else if (cmpval > 0)
  214.     {
  215.         runner = &(*runner)->left;
  216.     }
  217.     else /* cmpval < 0 */
  218.     {
  219.         runner = &(*runner)->right;
  220.     }
  221.     }
  222. }
  223.  
  224. void    DefineID(namestr, id)
  225. char    *namestr;
  226. NameTyp    id;
  227. /* DefineID:    like DefineName above, only using a different tree */
  228. {
  229.     InternalDefineName(&IDsTree, namestr, id);
  230. }
  231.  
  232.  
  233. void    PrintNameNode(node, depth)
  234. NameNodePtr    node;
  235. int depth;
  236. {
  237.     int i;
  238.  
  239.     if (!node) return;
  240.     PrintNameNode(node->left, depth+1);
  241.     for(i=0; i<depth; ++i)
  242.     {
  243.     printf(" ");
  244.     }
  245.     printf("str=\"%s\", id=%ld\n", node->stringPtr, node->id);
  246.     PrintNameNode(node->right, depth+1);
  247. }
  248.  
  249. void    PrintIDTree()
  250. /* print names and assoc. id #'s of variable ID tree */
  251. {
  252.     PrintNameNode(IDsTree, 0);
  253. }
  254.  
  255. void TestIDsTree()
  256. {
  257.     char s[256];
  258.     char str[256];
  259.     NameTyp id;
  260.  
  261.     fprintf(stderr, "testing GetInternalIDs, type a Var or Method number.\n");
  262.     while (gets(s))
  263.     {
  264.     if(1 == sscanf(s, "%s", str))
  265.     {
  266.         fprintf(stderr, "looking for %s.\n", str);
  267.         id = GetInternalID(str);
  268.         fprintf(stderr, "found %s, it is %ld.\n", str, id);
  269.     }
  270.     }
  271.     fprintf(stderr, "done.\n");
  272. }
  273.  
  274. void InitIDsTree()
  275. {
  276.     ParseScianIDs();
  277. }
  278.  
  279. /* old version, not using the auto-written ScianIDsDefiner.c */
  280. #if 0
  281. void    InitIDsTree()
  282. {
  283.     FILE *IDsFile;
  284.     char s[256];
  285.     char str[256];
  286.     NameTyp id;
  287.  
  288.     IDsTree = (NameNodePtr) NIL;
  289.  
  290.     /* attempt to open the file */
  291.     IDsFile = fopen("ScianIDs.h", "r");
  292.     if (! IDsFile)
  293.     {
  294.     return;
  295.     }
  296.  
  297.     /* read a line of the file */
  298.     while (fgets(s, 256, IDsFile))
  299.     {
  300.     if(2 == sscanf(s, "#define %s %ld", str, &id))
  301.     {
  302.         DefineID(str, id);
  303.     }
  304.     }
  305. }
  306. #endif
  307.  
  308. /* external defnition */
  309. int strcmp2(char *, char *);
  310.  
  311. NameTyp    GetInternalID(str)
  312. char    *str;
  313. {
  314.     return InternalNameToID(&IDsTree, str, strcmp2);
  315. }
  316.  
  317. char *GetInternalString(num)
  318. NameTyp num;
  319. {
  320.     return IDToName(num);
  321. }
  322.  
  323.